<template>
  <!--  -->
  <layout title="优惠券" background="#fff">
    <view class="coupon">
      <u-tabs
        :list="list"
        font-size="30"
        bar-height="12"
        bar-width="86"
        :bar-style="{
          'border-radius': '0',
          background: 'linear-gradient(90deg, #FFE6A3, #FFCB40)',
        }"
        active-color="#4E95FC"
        :is-scroll="false"
        :current="current"
        @change="change"
      ></u-tabs>
      <view class="coupon-list" v-show="current === 0">
        <template v-if="couponList.length">
          <view
            v-for="(item, index) in couponList"
            :key="index"
            class="coupon-item"
          >
            <view class="coupon-item-wrap">
              <view class="coupon-item-amount">
                <view class="amount">
                  <text>￥</text>
                  <text>{{ item.amount }}</text>
                </view>
                <view class="condition">{{ item.limitAmount }}</view>
              </view>
              <view class="coupon-item-info">
                <view class="info">
                  <view>{{ item.name }}</view>
                  <view>有效期：{{ item.endTime }}</view>
                </view>
                <view class="use" @tap="triggerToHome()">去使用</view>
              </view>
            </view>
          </view>
        </template>
        <view v-else class="empty-message"> 暂无数据 </view>
      </view>
      <view v-show="current === 1">
        <template v-if="invalidList.length">
          <view
            v-for="(item, index) in invalidList"
            :key="index"
            class="coupon-item unavailable"
          >
            <view class="coupon-item-wrap">
              <view class="coupon-item-amount">
                <view class="amount">
                  <text>￥</text>
                  <text>{{ item.amount }}</text>
                </view>
                <view class="condition">{{ item.condition }}</view>
              </view>
              <view class="coupon-item-info">
                <view class="info">
                  <view>{{ item.name }}</view>
                  <view>有效期：{{ item.endTime }}</view>
                </view>
              </view>
            </view>
            <view class="coupon-item-reason"> 不可用原因：{{ item.msg }} </view>
          </view>
        </template>
        <view v-else class="empty-message"> 暂无数据 </view>
      </view>
    </view>
  </layout>
</template>
<script>
export default {
  data() {
    return {
      list: [
        {
          name: "可使用",
        },
        {
          name: "不可使用",
        },
      ],
      couponList: [],
      invalidList: [],
      current: 0,
    };
  },
  onLoad(option) {
    this.triggerToFetchCouponList();
  },
  onShow() {},
  watch: {},
  methods: {
    async triggerToFetchCouponList() {
      const data = await this.$u.api.getCoupon();
      const { list1 = [], list2 = [] } = data.data;
      this.couponList = list1 || [];
      this.invalidList = list2 || [];
    },
    change(index) {
      this.current = index;
    },
    triggerToHome() {
      uni.navigateTo({
        url: "/pages/sys/home/index",
      });
    },
  },
};
</script>
<style lang="scss">
.coupon {
  .empty-message {
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    min-height: 50vh;
    font-size: 28rpx;
    font-family: PingFang SC;
    font-weight: 400;
    color: #4b4b4b;
    line-height: 36rpx;
  }
  &-list {
  }
  &-item {
    margin: 40rpx auto 0;
    width: 690rpx;
    background: #ffffff;
    box-shadow: 15rpx 26rpx 42rpx 8rpx #eceef0;
    border-radius: 20rpx;
    &-wrap {
      height: 160rpx;
      display: flex;
      justify-content: space-between;
    }
    &-info {
      margin-left: 23rpx;
      position: relative;
      border-top-right-radius: 20rpx;
      border-bottom-right-radius: 20rpx;
      padding: 43rpx 133rpx 41rpx 0;
      height: 100%;
      flex: 1;
      background-color: #fff;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .info {
        flex: 1;
        font-size: 34rpx;
        font-family: PingFang SC;
        font-weight: 400;
        color: #282828;
        line-height: 36rpx;
        & > view:last-child {
          margin-top: 20rpx;
          font-size: 24rpx;
          font-family: PingFang SC;
          font-weight: 400;
          color: #b5b5b5;
          line-height: 36rpx;
        }
      }
      .use {
        position: absolute;
        right: 26rpx;
        top: 50%;
        transform: translateY(-50%);
        font-size: 26rpx;
        font-family: PingFang SC;
        font-weight: 400;
        color: #ffffff;
        line-height: 52rpx;
        width: 107rpx;
        height: 52rpx;
        background: #4e95fc;
        border-radius: 26rpx;
        text-align: center;
      }
    }
    &-amount {
      padding: 39rpx 0rpx 40rpx 31rpx;
      min-width: 180rpx;
      flex-basis: 180rpx;
      width: 180rpx;
      height: 160rpx;
      background: linear-gradient(90deg, #4aabf6, #4675f6);
      border-radius: 20rpx 0rpx 0rpx 20rpx;
      .amount {
        font-size: 48rpx;
        font-family: PingFang SC;
        font-weight: 600;
        color: #ffffff;
        line-height: 36rpx;
        margin-bottom: 21rpx;
        & text:first-child {
          font-size: 36rpx;
        }
      }
      .condition {
        font-size: 24rpx;
        font-family: PingFang SC;
        font-weight: 400;
        color: #ffffff;
        line-height: 36rpx;
      }
    }
    &-reason {
      padding: 18rpx 23rpx;
      font-size: 24rpx;
      font-family: PingFang SC;
      font-weight: 400;
      color: #b5b5b5;
      line-height: 36rpx;
    }
    &.unavailable {
      .coupon-item-amount {
        border-bottom-left-radius: 0rpx;
        background: rgba(191, 191, 191, 0.5);
      }
      .coupon-item-info {
        border-bottom: 1px dashed #bfbfbf;
        border-bottom-right-radius: 0rpx;
      }
      .info {
        color: #282828;
        opacity: 0.5;
        & > view:last-child {
          color: #b5b5b5;
        }
      }
    }
  }
}
</style>
